<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\rehearse\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 救援行动管理
 * @package app\record
 * @author LJQ<245369209@qq.com>
 */
class Record extends Api

{

    /**
   *  救援行动列表
   * @param string 
   * @return Json
   */

  public function recordList($recordId=null){
              
              $where= [] ;
              $with='events,records';
              $where['typeId']=2;
              
               if ($recordId) {
                      $where['id']=['eq',$recordId];  
                  }
              
    //分页参数
    if(($limit = input('limit')) && ($page = input('page'))){
      $startLimit =  ($page-1)*$limit;
      // 救援行动列表
      $info =collection(model('record')->with('events')->with(['records'=>function($query){$query->where('actionStatus','<>',0);}])->with('records.unit')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
    }else{
      //救援行动列表
      $info =collection(model('record')->with('events')->with(['records'=>function($query){$query->where('actionStatus','<>',0);}])->with('records.unit')->where($where)->select())->toArray();
    }
       
    //总数
       $count = model('record')->where($where)->count('id'); 
    
              if ($info && $count == 0) {
              return json(['code'=>1,'msg'=>'没有找到救援行动','count'=>$count,'data'=>$info]);
          } 
        
          if ($count > 0){
            return json(['code'=>1,'msg'=>'救援行动','count'=>$count,'data'=>$info]);
          }
                return json(['code'=>0,'msg'=>'数据异常']);
       }
      
          //添加救援行动
     public function add($data){
          
          if (empty($data)) {
              return json(['code'=>0,'msg'=>'数据异常,请检查']);
          }

          $model = model('record');
       
          $result = $model->change($data);
          if (false != $result) {
            setlog('recordadd','record',session('user_auth.id'));
              return json(['code'=>1,'msg'=>'救援行动添加成功','recordId'=>$model->id]);
          } else {
              return json(['code'=>0,'msg'=>'救援行动添加失败,请检查']);
          }
      }

    

   /**
   * 修改救援行动
   * @param  string recordId 救援行动id,
   * @return Json
   */
    public function edit($data,$recordId){
        
          if (empty($recordId)) {
              return json(['code'=>0,'msg'=>'救援行动不存在,请检查']);
          }
          $model = model('record/record');     
          if ($model->change($data)) {
             setlog('recordupdate','record',session('user_auth.id'));
             return json(['code'=>1,'msg'=>'救援行动修改成功']);
            }else{
             return json(['code'=>0,'msg'=>'救援行动修改失败,请检查']);
            }
    }
    /**
   *  删除救援行动
   * @param string recordId 救援行动id,
   * @param Json
   */
     
    public function del($recordId) {
        $recordId = input('recordId',0,'intval');
        $model = model('record');
        if (empty ( $recordId )||empty(($model->where(array('id'=>$recordId))->find()))) {
            return  json(['code'=>0,'msg'=>'救援行动id不存在,请检查']);
        }
        if($model->destroy(['id'=>$recordId]) !== FALSE){
            $result=model('recorddetail')->where(array('rescueExecId'=>$recordId))->delete();
            setlog('recorddel','record',session('user_auth.id'));
           return json(['code'=>1,'msg'=>'救援行动删除成功']);
       }else{
           return $this->json(['code'=>0,'msg'=>'救援行动删除失败,请检查']);
       }
    }

     
   /**
   *   查询救援行动
   * @param keyword救援行动名称
   * @return Json
   */
    public function search($start=null,$end=null,$keyword=null){
         
          $where= [] ;
          $with='events,records';
          $where['typeId']=2;

          if($start&&$end){
                 $start=strtotime($start);
                 $end=strtotime($end);
                 $where['execTime'] = ['between time',[$start,$end]];
            }



      //分页参数
    if(($limit = input('limit')) && ($page = input('page'))){
      $startLimit =  ($page-1)*$limit;
      //救援行动列表
    $info =collection(model('record')->hasWhere('events',['title'=>['like',"%$keyword%"]])->with($with)->where($where)->limit($startLimit.','.$limit)->select())->toArray();
   
      
    }else{
      //救援行动列表
      $info =collection(model('record')->hasWhere('events',['title'=>['like',"%$keyword%"]])->with($with)->where($where)->select())->toArray();
    }
    //总数
         $count = model('record')->where($where)->count('id'); 
        
     
             if (empty($info) && $count == 0) {
                return json(['code'=>0,'msg'=>'没有找到救援行动','count'=>$count]);
        } 
        
        if ($count > 0){
          return json(['code'=>1,'msg'=>'救援行动','count'=>$count,'data'=>$info]);
        }
      } 


      
}